/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Calendario.java
 *
 * Created on 11-oct-2009, 12:16:56
 */

package utilidades;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.*;
/**
 *
 * @author carlos
 */
public class Calendario extends javax.swing.JDialog implements ActionListener{

    /** Creates new form Calendario */
    int mapa[][];
    String fecha;
    JTextField tf=new JTextField();
    int min=0;
    int max=1000000;
  
    public Calendario(java.awt.Frame parent, boolean modal,JTextField tf,int mes,int ano) {
        super(parent, modal);
        initComponents();
        this.tf=tf;
        setSize(380,390);
        anoo.setText(ano+"");
        mess.setSelectedIndex(mes-1);
        crearCalendario(ano+"/"+mes+"/01");
    }

    int fechafin=0;
    int fechain=0;
    int f=0;
       public Calendario(java.awt.Frame parent, boolean modal,JTextField tf,int mes,int ano,String fechain,String fechafin) {
        super(parent, modal);
        initComponents();
        this.tf=tf;
        this.fechafin=Integer.parseInt(fechafin.replace("-",""));
        this.fechain=Integer.parseInt(fechain.replace("-",""));
        f=1;
        setSize(380,390);
  anoo.setText(ano+"");
  mess.setSelectedIndex(mes-1);
        crearCalendario(ano+"/"+mes+"/01");
    }

 public Calendario(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setTitle("Calendario");
        int ano=Calendar.getInstance().get(Calendar.YEAR);
        int mes=Calendar.getInstance().get(Calendar.MONTH);
        anoo.setText(ano+"");
        mess.setSelectedIndex(mes);
        System.out.println(ano+"/"+mes+"/"+01);
        crearCalendario(ano+"/"+(mes+1)+"/01");
        setSize(380,380);

    }

public String getFecha(){

        this.setVisible(true);

        return fechaseleccionada;
}

    public String getDia(int dia){
        switch(dia){
            case 0:
                return "Do";
            case 1: return "Lu";
            case 2: return "Ma";
            case 3: return "Mi";
            case 4: return "Ju";
            case 5: return "Vi";
            case 6: return "Sa";
            default: return "no existe";
        }
    }

    public void crearCalendario(String fecha_in){
        Fechavo f=new Fechavo();
        int dia=new Date(fecha_in).getDay();
        int mes=new Date(fecha_in).getMonth();
        for(int i=0;i<8;i++){
            JLabel l=new JLabel(getDia(i));
            l.setBounds((i*50)+30,0,50,40);
            panel.add(l);
        }

        for(int i=0;i<6;i++){
            for(int j=0;j<7;j++){
                dia=(new Date(fecha_in).getDay());
                if(mes==(new Date(fecha_in).getMonth()))
                if(dia==j){
                    JButton bt=new JButton(new Date(fecha_in).getDate()+"");
                    bt.setBounds(10+(j*50),(i*40)+30,50,40);
                    bt.addActionListener(this);
                    panel.add(bt);
                    fecha_in=f.fechaSiguiente(fecha_in);
                }

            }
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mess = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        panel = new javax.swing.JPanel();
        anoo = new javax.swing.JTextField();
        menos = new javax.swing.JButton();
        mas = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(null);

        mess.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre" }));
        mess.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                messItemStateChanged(evt);
            }
        });
        getContentPane().add(mess);
        mess.setBounds(230, 20, 100, 22);

        jLabel1.setText("Año");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(20, 20, 30, 14);

        jLabel2.setText("Mes");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(200, 20, 40, 14);

        javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
        panel.setLayout(panelLayout);
        panelLayout.setHorizontalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 390, Short.MAX_VALUE)
        );
        panelLayout.setVerticalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 290, Short.MAX_VALUE)
        );

        getContentPane().add(panel);
        panel.setBounds(0, 60, 390, 290);

        anoo.setEditable(false);
        anoo.setText("2009");
        getContentPane().add(anoo);
        anoo.setBounds(90, 20, 50, 20);

        menos.setText("<");
        menos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menosActionPerformed(evt);
            }
        });
        getContentPane().add(menos);
        menos.setBounds(70, 20, 20, 20);

        mas.setText(">");
        mas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                masActionPerformed(evt);
            }
        });
        getContentPane().add(mas);
        mas.setBounds(140, 20, 20, 20);

        jLabel3.setText("<<");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(50, 20, 34, 14);

        jLabel4.setText(">>");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(160, 20, 34, 14);

        pack();
    }// </editor-fold>//GEN-END:initComponents
String fechaseleccionada="";
    public void actionPerformed(ActionEvent e){
    //JOptionPane.showMessageDialog(null,((JButton)e.getSource()).getText());
    String mes=(mess.getSelectedIndex()+1)+"";
    if(mes.length()==1)
        mes=0+mes;  

     String dias=((JButton)e.getSource()).getText();
    if(dias.length()==1)
        dias=0+dias;
   if(f==1){
       int fech=Integer.parseInt(anoo.getText()+mes+dias);
       if(fech>=fechain&&fech<=fechafin){
            this.tf.setText(anoo.getText()+"-"+mes+"-"+dias);
            fechaseleccionada=anoo.getText()+"-"+mes+"-"+dias;
        this.dispose();
       }else
           JOptionPane.showMessageDialog(null,"La fecha esta fuera del rango");


   }else{
          this.tf.setText(anoo.getText()+"-"+mes+"-"+dias);
          fechaseleccionada=anoo.getText()+"-"+mes+"-"+dias;
        this.dispose();
   }
}

public void actualizar(){
     panel.removeAll();
        panel.repaint();
      //  System.out.println(anoo.getValue()+"/"+(mess.getSelectedIndex()+1)+"/01");
         crearCalendario(anoo.getText()+"/"+(mess.getSelectedIndex()+1)+"/01");

}

    private void messItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_messItemStateChanged
        // TODO add your handling code here:
        panel.removeAll();
        panel.repaint();
      //  System.out.println(anoo.getValue()+"/"+(mess.getSelectedIndex()+1)+"/01");
         crearCalendario(anoo.getText()+"/"+(mess.getSelectedIndex()+1)+"/01");
}//GEN-LAST:event_messItemStateChanged

    private void masActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_masActionPerformed
        // TODO add your handling code here:
         if((Integer.parseInt(anoo.getText())+1)<=max){
             anoo.setText((Integer.parseInt(anoo.getText())+1)+"");
              actualizar();
          }else
            JOptionPane.showMessageDialog(this.getParent(),"El año esta fuera del limite");

    }//GEN-LAST:event_masActionPerformed

    private void menosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menosActionPerformed
        // TODO add your handling code here:
        if((Integer.parseInt(anoo.getText())-1)>=min){
         anoo.setText((Integer.parseInt(anoo.getText())-1)+"");
         actualizar();
        }else
            JOptionPane.showMessageDialog(this.getParent(),"El año esta fuera del limite");
    }//GEN-LAST:event_menosActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        new Calendario(new JFrame(),true,null,10,2009).setVisible(true);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField anoo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JButton mas;
    private javax.swing.JButton menos;
    private javax.swing.JComboBox mess;
    private javax.swing.JPanel panel;
    // End of variables declaration//GEN-END:variables

}
